package com.xiaoxi.bms.service.quick.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.system.CodeModelSystemEnums;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.user.CodeModelUserEnums;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.domain.quick.BmsCodeModel;
import com.xiaoxi.bms.domain.quick.dto.BMSCodeModelDTO;
import com.xiaoxi.bms.domain.quick.vo.BMSCodeModelVO;
import com.xiaoxi.bms.mapper.quick.BmsCodeModelMapper;
import com.xiaoxi.bms.service.common.circulation.ExportDataToExcelService;
import com.xiaoxi.bms.service.quick.BmsCodeModelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName BmsCodeModelServiceImpl
 * @Description
 * @Author xiaoxi
 * @Date 2022-11-05
 * @Version 1.0
 */
@Service("bmsCodeModelServiceImpl")
public class BmsCodeModelServiceImpl extends ServiceImpl<BmsCodeModelMapper, BmsCodeModel> implements BmsCodeModelService, ExportDataToExcelService {

    @Override
    public IPage<BMSCodeModelVO> findListByPage(BMSCodeModelDTO bmsCodeModelDTO, Pageable pageable) {

        IPage<BmsCodeModel> iPage = new Page<>(pageable.getPageNumber(), pageable.getPageSize());

        QueryWrapper<BmsCodeModel> queryWrapper = new QueryWrapper<>();


        if (StringUtils.isNotBlank(bmsCodeModelDTO.getCodeType())) {
            queryWrapper.like("code_type", bmsCodeModelDTO.getCodeType());
        }

        if (StringUtils.isNotBlank(bmsCodeModelDTO.getSeqType())) {
            queryWrapper.eq("seq_type", bmsCodeModelDTO.getSeqType());
        }

        if (StringUtils.isNotBlank(bmsCodeModelDTO.getName())) {
            queryWrapper.eq("name", bmsCodeModelDTO.getName());
        }

        if (Objects.nonNull(bmsCodeModelDTO.getCategories())) {
            queryWrapper.eq("categories", bmsCodeModelDTO.getCategories());
        }

        queryWrapper.orderByDesc("modify_time");

        iPage = baseMapper.selectPage(iPage, queryWrapper);

        List<BMSCodeModelVO> bmsCodeModelVOList = new ArrayList<>();
        iPage.getRecords().forEach(bmsCodeModel -> {
            BMSCodeModelVO bmsCodeModelVO = new BMSCodeModelVO();
            BeanUtils.copyProperties(bmsCodeModel, bmsCodeModelVO);
            bmsCodeModelVOList.add(bmsCodeModelVO);
        });
        IPage<BMSCodeModelVO> bmsCodeModelVOIPage = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        bmsCodeModelVOIPage.setRecords(bmsCodeModelVOList);
        bmsCodeModelVOIPage.setTotal(iPage.getTotal());
        return bmsCodeModelVOIPage;
    }

    @Override
    public void add(BMSCodeModelVO bmsCodeModelVO) throws BMSException {

        if (checkCodeType(bmsCodeModelVO.getCodeType(), null)) {
            throw new BMSException(CodeModelUserEnums.CODE_TYPE_EXIST);
        }

        if (checkName(bmsCodeModelVO.getName(), null)) {
            throw new BMSException(CodeModelUserEnums.CODE_NAME_EXIST);
        }

        BmsCodeModel bmsCodeModel = new BmsCodeModel();
        BeanUtils.copyProperties(bmsCodeModelVO, bmsCodeModel);
        baseMapper.insert(bmsCodeModel);
    }

    @Override
    public void delete(Long id) throws BMSException {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(BMSCodeModelVO bmsCodeModelVO) throws BMSException {

        if (checkCodeType(bmsCodeModelVO.getCodeType(), bmsCodeModelVO.getId())) {
            throw new BMSException(CodeModelUserEnums.CODE_TYPE_EXIST);
        }

        if (checkName(bmsCodeModelVO.getName(), bmsCodeModelVO.getId())) {
            throw new BMSException(CodeModelUserEnums.CODE_NAME_EXIST);
        }

        BmsCodeModel bmsCodeModel = new BmsCodeModel();
        BeanUtils.copyProperties(bmsCodeModelVO, bmsCodeModel);
        baseMapper.updateById(bmsCodeModel);
    }

    @Override
    public BmsCodeModel findById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Boolean checkCodeType(String codeType, Long id) {
        QueryWrapper<BmsCodeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code_type", codeType);
        if (Objects.nonNull(id)) {
            queryWrapper.ne("id", id);
        }
        return baseMapper.selectCount(queryWrapper) != 0;
    }

    @Override
    public Boolean checkName(String name, Long id) {
        QueryWrapper<BmsCodeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        if (Objects.nonNull(id)) {
            queryWrapper.ne("id", id);
        }
        return baseMapper.selectCount(queryWrapper) != 0;
    }

    @Override
    public void enableCodeModel(Long id) throws BMSException {
        BmsCodeModel codeModel = baseMapper.selectById(id);
        if (Objects.isNull(codeModel)) {
            throw new BMSException(CodeModelSystemEnums.CODE_ID_NOT_EXIST);
        }
        BmsCodeModel bmsCodeModel = new BmsCodeModel();
        bmsCodeModel.setId(id);
        bmsCodeModel.setEnable(!codeModel.getEnable());
        baseMapper.updateById(bmsCodeModel);
    }

    @Override
    public BmsCodeModel getCodeModel(String codeType) {
        QueryWrapper<BmsCodeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code_type", codeType);
        return baseMapper.selectOne(queryWrapper);
    }


    @Override
    public List<String> tableHeader(String... args) {
        List<String> tableHeader = new ArrayList<>();

        tableHeader.add("表名");

        return tableHeader;
    }

    @Override
    public <T> IPage<T> tableContent(T objDTO, Pageable pageable, String... args) {
        return (IPage<T>) findListByPage((BMSCodeModelDTO) objDTO, pageable);
    }
}
